/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//this function only for basic_setting
function LoadLocation( province, city ){
	var options = $("#selprovince" ).html();
	for ( var key in cityMap ){
		if( key == province )
			options += "<option value='" + key + "' selected>" +  key +"</option>";
		else
			options += "<option value='" + key + "'>" +  key +"</option>";
	}
	
	$("#selprovince").html( options );
	
	$("#selprovince").change(
		function()
		{
			var citys = "<option>选择城市</option>";
			var pro = $("#selprovince option:selected").get(0).value;
			if( pro != '选择省份' ){
				for( i = 0; i < cityMap[pro].length; i++ )
				{
					citys += "<option value='" + cityMap[pro][i] + "'>" + cityMap[pro][i] + "</option>";
				}
				$("#selcity").html( citys );
				$("#province").val( pro );
			}
			else{
				$("#selcity").html( citys );
			}
		} 
		);
	$("#selcity").change(
		function()
		{
			$("#city").val( $("#selcity option:selected").get(0).value );
		}
	);
	

	//init selection
	var curprovince = $("#selprovince option:selected").get(0).value;
	if( curprovince != '选择省份' ){
		var citys = "";
		var pro = $("#selprovince option:selected").get(0).value;
		for( i = 0; i < cityMap[pro].length; i++ )
		{
			if( city == cityMap[pro][i] )
				citys += "<option value='" + cityMap[pro][i] + "' selected>" + cityMap[pro][i] + "</option>";
			else
				citys += "<option value='" + cityMap[pro][i] + "'>" + cityMap[pro][i] + "</option>";
		}
		$("#selcity").html( citys );
	}
	//default selection
	else{
		$("#selprovince").val('0');
		$("#selprovince").change();
	}
}

function SetBirthOption()
{
	var monthOption="";
	var dayOption="";
	var yearVal=$("#selyear option:selected").get(0).value;
	if(yearVal!='0000')
	{
		for(i=1; i<=12; i++)
		{
			if(i<10)
				monthOption += "<option value=\"0" + i + "\">0" + i + "</option>";
			else
				monthOption += "<option value=\"" + i + "\">" + i + "</option>";;
		}
		for(i=1; i<=31; i++)
		{
			if(i<10)
				dayOption += "<option value=\"0" + i + "\">0" + i + "</option>";
			else
				dayOption += "<option value=\"" + i + "\">" + i + "</option>";;
		}
		$("#selmonth").html( monthOption );
		$("#selday").html( dayOption );
		$("#selyear").val( yearVal );
	}
	else
	{
		monthOption = "<option value=\"00\">保密</option>";
		dayOption = "<option value=\"00\">保密</option>";
		$("#selmonth").html( monthOption );
		$("#selday").html( dayOption );
	}
}

function LoadDetailSetting(){

	var birthday = $( "#birthday" ).val();
	var arrBirthHeader = birthday.split( " " );
	var arrBirth = arrBirthHeader[0].split( "-" );

	//year
	$( "#selyear" ).val( arrBirth[0]);
	SetBirthOption();
	$( "#selmonth" ).val( arrBirth[1] );
	$( "#selday" ).val( arrBirth[2] );

	$("#selsalary").val( $("#salary").val() );
	$("#seleducation").val( $("#education").val() );
	$("#selprofession").val( $("#profession").val() );

	$("#selyear").change(function()
	{
		SetBirthOption();
	});
	
	$("#selsalary").change(function(){
		$("#salary").val( $("#selsalary option:selected").get(0).value );
	});
	$("#seleducation").change(function(){
		$("#education").val( $("#seleducation option:selected").get(0).value );
	});
	$("#selprofession").change(function(){
		$("#profession").val( $("#selprofession option:selected").get(0).value );
	});
}

function BasicInfoFormValidation(){
	$("#basicInfoForm").validate({
		rules: {      
			introduction: { maxlength: 800 }
		},
		messages: {
			introduction: { maxlength: "最大长度不超过800字符" }
		},
		submitHandler: function(form) {
			return true;
		}
	});
}

function DetailInfoFormValidation(){
	$("#detailInfoForm").validate({
		rules: {
			qq: { number: true, minlength: 5, maxlength: 10 },
			msn:{ email : true, maxlength: 40 },
			shopping_method : { maxlength: 180 },
			shopping_plat : { maxlength: 180 },
			interested_cate : { maxlength: 180 },
			interested_grand : { maxlength: 180 }
		},
		messages: {
			qq: { number: "QQ必须为数字" , minlength: "QQ位数为5至10位", maxlength: "QQ位数为5至10位" },
			msn:{ email : "MSN格式不正确", maxlength: "MSN最大长度不超过40字符" },
			shopping_method : { maxlength: "最大长度不超过180字符"},
			shopping_plat : { maxlength: "最大长度不超过180字符" },
			interested_cate : { maxlength: "最大长度不超过180字符" },
			interested_grand : { maxlength: "最大长度不超过180字符" }
		},
		submitHandler: function(form) {
			return true;
		}
	});
}

function AccountInfoFormValidation(){
	$("#accountInfoForm").validate({
		rules: {
			email: { required: true, email: true, maxlength: 50, check_other_email: true },
			oldpwd: { check_oldpwd: true },
			newpwd: { minlength: 6, maxlength: 20},
			newpwdagain: { minlength: 6, maxlength: 20, equalTo:"#newpwd" },
			name: { maxlength: 18 },
			address:{ maxlength: 180 },
			zip : { number:true, rangelength: [6,6] },
			phone: { maxlength: 18  }
		},
		messages: {
			email: { required:"需要输入电子邮箱", email:"电子邮箱格式不正确", maxlength:'最多输入50个字符',check_other_email:'该邮箱地址已经存在'},
			oldpwd: { check_oldpwd: "旧密码不正确" },
			newpwd: { minlength:'密码最少6个字符', maxlength:'密码最多20个字符'},
			newpwdagain: { minlength:'密码最少6个字符', maxlength:'密码最多20个字符', equalTo:"两次输入的密码不一致"},
			name: { maxlength: "姓名不能超过18个字符" },
			address:{ maxlength: "地址不能超过180个字符"  },
			zip : { number:"邮政编码必须为数字", rangelength: "请输入6位邮政编码" },
			phone: { maxlength:"电话号码不能超过18个字符"  }
		},
		 //获得焦点时不验证
        focusInvalid: false,   
        onkeyup: false,
		submitHandler: function(form) {
			return true;
		}
	});
}

function LoadAccountInfoLogic(){
	$("#changeemail").click(
		function(){
			$("#email").val( $("#orignalEmail").val() );
			if( $("#emailinput").is(':visible') ){
				$("#emaillabel").show();
				$("#emailinput").hide();
				$("#email").attr('disabled', true);
				$("#changeemail").html( "修改" );
			}
			else{
				$("#emaillabel").hide();
				$("#emailinput").show();
				$("#email").removeAttr('disabled');
				$("#changeemail").html( "放弃" );
			}
		}
	);

	$("#changepwd").click(
		function(){
			if( $("#pwdinput").is(':visible') ){
		
				$("#pwdlabel").show();
				$("#pwdinput").hide();
				$("#pwdinput1").hide();
				$("#pwdinput2").hide();
				$("#oldpwd").attr('disabled', true);
				$("#newpwd").val("");
				$("#newpwdagain").val("");
				$("#changepwd").html( "修改" );
			}
			else{
			
				$("#pwdlabel").hide();	
				$("#pwdinput").show();
				$("#pwdinput1").show();
				$("#pwdinput2").show();						
				$("#oldpwd").removeAttr('disabled');
				$("#changepwd").html( "放弃" );
			}
		}
	);
}
var firstFocus = 0;
$(document).ready(
	function(){
		//add new validation check method for telphone
		jQuery.validator.addMethod("phone", function(value) {
			return /^[0-9\-+]{7,18}$/.test(value);
		}, '请输入正确的电话号码');

		//Validate Function to check the email address overlap
		jQuery.validator.addMethod("check_other_email", function(value)
		{
			var result=true;
			$.ajax({
				type: 'Get',
				url: edp_domain_url + 'ajax_register_check/?tiny=1&type=other_email&email='+value+'&id='+$("#id").val(),
				async: false,
				dataType: 'json',
				timeout: 5000,
				success: function(data)
				{
					result=data.res;
				}
			});
			return result;
		}, "");

		//Validate Function to check the email address overlap
		jQuery.validator.addMethod("check_oldpwd", function(value)
		{
			var result=true;
			$.ajax({
				type: 'Get',
				url: edp_domain_url + 'ajax_register_check/?tiny=1&type=pwd&pwd='+value+'&id='+$("#id").val(),
				async: false,
				dataType: 'json',
				timeout: 5000,
				success: function(data)
				{
					result=data.res;
				}
			});
			return result;
		}, "");

		var currentSubpage = $( "#currentSubpage" ).val();
		if( currentSubpage == "basic_info" ){
			LoadLocation($("#province").val(),$("#city").val());
			BasicInfoFormValidation();
		}
		else if( currentSubpage == "detail_info" ){
	
			LoadDetailSetting();
			DetailInfoFormValidation();
		}	
		else if( currentSubpage == "account_info" ){
			LoadAccountInfoLogic();
			AccountInfoFormValidation();
		}
		
		$( "#"+currentSubpage ).addClass( "nav_selected" );

		//set href to current click disabled
		$( "#"+currentSubpage ).click(
			function(){
				return false;
			}
		);
		
		$("#ori_pic").Jcrop({ 
			onChange:showPreview, 
			onSelect:showPreview, 
			aspectRatio:1
		}); 
		
		
		$("#blockusername").focus(function(){
				if( firstFocus == 0 ){
					$(this).val('');
					$(this).css('color', 'black' );
					firstFocus = 1;
				}
		});		
		
	}
);

function showPreview(coords){ 
	if(parseInt(coords.w) > 0){ 
		var rx = $("#preview_box").width() / coords.w; 
		var ry = $("#preview_box").height() / coords.h; 
		$("#crop_preview").css({ 
			width:Math.round(rx * $("#ori_pic").width()) + "px", 
			height:Math.round(rx * $("#ori_pic").height()) + "px", 
			marginLeft:"-" + Math.round(rx * coords.x) + "px", 
			marginTop:"-" + Math.round(ry * coords.y) + "px" });
		
		$("#x1").val(coords.x); 
		$("#y1").val(coords.y); 
		$("#x2").val(coords.x2); 
		$("#y2").val(coords.y2); 
		$("#w").val(coords.w); 
		$("#h").val(coords.h); 
	}
}
